import { Component, OnInit } from '@angular/core';
import { List } from 'linqts';
import { PowerLoss } from "../../../entity/power-loss.model";
import { PowerLossRankService } from "../../../service/power-loss-rank.service";

@Component({
	selector: 'app-performance-list',
	templateUrl: './performance-list.component.html',
	styleUrls: ['./performance-list.component.css']
})
export class PerformanceListComponent implements OnInit {

	//年度风场电量损失集合 / 年度风机电量损失集合排序数据
	private originalFarmLossYear: PowerLoss[] = [];
	private farmLossYearData: PowerLoss[] = [];

	private originalTurbLossYear: PowerLoss[] = [];
	private turbineLossYearData: PowerLoss[] = [];

	//季度风场电量损失集合 / 季度风机电量损失集合排序数据
	private originalFarmLossQuarter: PowerLoss[] = [];
	private farmLossQuarterData: PowerLoss[] = [];

	private originalTurbLossQuarter: PowerLoss[] = [];
	private turbineLossQuarterData: PowerLoss[] = [];

	//月度风场电量损失集合 / 月度风机电量损失集合排序数据
	private originalFarmLossMonth: PowerLoss[] = [];
	private farmLossMonthData: PowerLoss[] = [];

	private originalTurbLossMonth: PowerLoss[] = [];
	private turbineLossMonthData: PowerLoss[] = [];

	//当前风场下风机年度/季度/月度电量损失集合
	private originalFarmTurbLossYear: PowerLoss[] = [];
	private farmTurbLossYear: PowerLoss[] = [];

	private originalFarmTurbLossQuarter: PowerLoss[] = [];
	private farmTurbLossQuarter: PowerLoss[] = [];

	private originalFarmTurbLossMonth: PowerLoss[] = [];
	private farmTurbLossMonth: PowerLoss[] = [];


	/**
	   * 时间当前选中下标
	   * 
	   * @type {number}@memberof PerformanceListComponent
	   */
	public active: number = 0;

	/**
	 * 损失电量当前选中下标(0,1,2,3,4)
	 * 
	 * @type {number}@memberof PerformanceListComponent
	 */
	public powerLossType: number = 0;

	//时间类型(年、季度、月)
	private dateType: number;

	//风场id
	private farmId: number[] = [];

	//tab切换对应损失电量
	public powerLossStr: string = "实发电量";

	//时间选择
	public years: string[] = ["2020", "2019", "2018", "2017", "2016", "2015", "2014", "2013", "2012", "2011", "2010"];
	public currentYear: string = new Date().getFullYear().toString();
	public months: string[] = ["12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1"];
	public currentMonth: string = (new Date().getMonth()+1).toString();;
	public quarters: number[] = [4, 3, 2, 1];
	public currentQuarter: number = this.quarters[1];

	//排名1,2标识
	public ranks: string[] = ['前', '后']
	public currentRank: string = this.ranks[0];
	private rankType: number = 1;
	public rankValue: number = 20;

	/**
	 * echart series(年)
	 * 
	 * @type {any[]}@memberof TurbineTrendComponent
	 */
	yearFarmSeries = [];
	yearFarmXAxis: string[] = [];

	yearTurbSeries = [];
	yearTurbXAxis: string[] = [];

	/**
	 * echart series(季度)
	 * 
	 * @type {any[]}@memberof TurbineTrendComponent
	 */
	qurterFarmSeries = [];
	qurterFarmXAxis: string[] = [];

	qurterTurbSeries = [];
	qurterTurbXAxis: string[] = [];

	/**
	 * echart series(月度)
	 * 
	 * @type {any[]}@memberof TurbineTrendComponent
	 */
	monthFarmSeries = [];
	monthFarmXAxis: string[] = [];

	monthTurbSeries = [];
	monthTurbXAxis: string[] = [];

	constructor(
		private powerLossRankService: PowerLossRankService,
	) { }

	ngOnInit() {
		this.onSearch(this.active);
	}

	public onClickHeader(n: number) {
		this.active = n;
		this.onSearch(this.active);
	}
	public onChangeYear(year: string) {
		this.currentYear = year;
	}
	public onChangeQuarter(quarter: number) {
		this.currentQuarter = quarter;
	}
	public onChangeMonth(month: string) {
		this.currentMonth = month;
	}
	public onChangeRank(event) {
		this.currentRank = event;
		if (this.currentRank == '前') {
			this.rankType = 1;
		} else {
			this.rankType = 2;
		}
	}
	public onClickLoss(n: number) {
		this.powerLossType = n;
		if (this.powerLossType == 0) {
			this.powerLossStr = "实发电量"
		} else if (this.powerLossType == 1) {
			this.powerLossStr = "停机损失"
		} else if (this.powerLossType == 2) {
			this.powerLossStr = "性能损失"
		} else if (this.powerLossType == 3) {
			this.powerLossStr = "限电损失"
		} else {
			this.powerLossStr = "坏点损失"
		}
		this.onSearch(this.active);
	}

	public onFarmColumnClick(event) {
		this.farmId = [];
		this.farmId.push(event);
		this.getFarmPowerLossList(this.active);
	}

	public onSearch(active: number) {
		this.initEchartsData(active);
	}

	private initEchartsData(active: number) {
		if (active == 0) {
			this.dateType = 3;
			let year = new Date(this.currentYear);
			this.originalFarmLossYear = [];
			this.farmLossYearData = [];
			this.originalTurbLossYear = [];
			this.turbineLossYearData = [];
			Promise.all(
				[this.powerLossRankService.getFarmPowerLossRankList(this.farmId, this.powerLossType, this.dateType, year),
				this.powerLossRankService.getTurbinePowerLossRankList(this.farmId, this.powerLossType, this.dateType, year, this.rankType, this.rankValue),
				])
				.then(res => {
					if (res[0]) {
						this.originalFarmLossYear = [...res[0]];
						this.farmLossYearData.push(...this.originalFarmLossYear);
					};
					if (res[1]) {
						this.originalTurbLossYear = [...res[1]];
						this.turbineLossYearData.push(...this.originalTurbLossYear);
					};
					let seriesObj: SeriesObj;
					if (this.farmLossYearData != null && this.turbineLossYearData != null) {
						seriesObj = this.getColumnSeries(this.farmLossYearData, this.turbineLossYearData);

						this.yearFarmSeries = seriesObj.farmSeries;
						this.yearFarmXAxis = seriesObj.farmXAxis;
						this.yearTurbSeries = seriesObj.turbSeries;
						this.yearTurbXAxis = seriesObj.turbXAxis.map(function (str) {
							return str.replace(' ', '\n');
						})
					}
				})
				.catch(error => {
					console.log("app-performance-list has error", error);
				});
		}
		else if (active == 1) {
			this.originalFarmLossQuarter = [];
			this.farmLossQuarterData = [];
			this.originalTurbLossQuarter = [];
			this.turbineLossQuarterData = [];
			this.dateType = 4;
			let quarterDate: Date;

			if (this.currentQuarter == 1) {
				quarterDate = new Date(this.currentYear)
			} else if (this.currentQuarter == 2) {
				quarterDate = new Date(this.currentYear + '-' + '4')
			} else if (this.currentQuarter == 3) {
				quarterDate = new Date(this.currentYear + '-' + '7')
			} else {
				quarterDate = new Date(this.currentYear + '-' + '10')
			}

			Promise.all(
				[this.powerLossRankService.getFarmPowerLossRankList(this.farmId, this.powerLossType, this.dateType, quarterDate),
				this.powerLossRankService.getTurbinePowerLossRankList(this.farmId, this.powerLossType, this.dateType, quarterDate, this.rankType, this.rankValue),
				])
				.then(res => {
					if (res[0]) {
						this.originalFarmLossQuarter = [...res[0]];
						this.farmLossQuarterData.push(...this.originalFarmLossQuarter);
					};
					if (res[1]) {
						this.originalTurbLossQuarter = [...res[1]];
						this.turbineLossQuarterData.push(...this.originalTurbLossQuarter);
					};

					let seriesObj: SeriesObj;
					if (this.farmLossQuarterData != null && this.turbineLossQuarterData != null) {

						seriesObj = this.getColumnSeries(this.farmLossQuarterData, this.turbineLossQuarterData);

						this.qurterFarmSeries = seriesObj.farmSeries;
						this.qurterFarmXAxis = seriesObj.farmXAxis;
						this.qurterTurbSeries = seriesObj.turbSeries;
						this.qurterTurbXAxis = seriesObj.turbXAxis.map(function (str) {
							return str.replace(' ', '\n');
						});
					}

				})
				.catch(error => {
					console.log("app-performance-list has error", error);
				});

		}
		else {
			this.originalFarmLossMonth = [];
			this.farmLossMonthData = [];
			this.originalTurbLossMonth = [];
			this.turbineLossMonthData = [];

			let month = new Date(this.currentYear + "-" + this.currentMonth);
			this.dateType = 2;

			Promise.all(
				[this.powerLossRankService.getFarmPowerLossRankList(this.farmId, this.powerLossType, this.dateType, month),
				this.powerLossRankService.getTurbinePowerLossRankList(this.farmId, this.powerLossType, this.dateType, month, this.rankType, this.rankValue),
				])
				.then(res => {
					if (res[0]) {
						this.originalFarmLossMonth = [...res[0]];
						this.farmLossMonthData.push(...this.originalFarmLossMonth);
					};
					if (res[1]) {
						this.originalTurbLossMonth = [...res[1]];
						this.turbineLossMonthData.push(...this.originalTurbLossMonth);
					};
					let seriesObj: SeriesObj;

					if (this.farmLossMonthData != null && this.turbineLossMonthData != null) {

						seriesObj = this.getColumnSeries(this.farmLossMonthData, this.turbineLossMonthData);

						this.monthFarmSeries = seriesObj.farmSeries;
						this.monthFarmXAxis = seriesObj.farmXAxis;
						this.monthTurbSeries = seriesObj.turbSeries;
						this.monthTurbXAxis = seriesObj.turbXAxis.map(function (str) {
							return str.replace(' ', '\n');
						});
					}

				})
				.catch(error => {
					console.log("app-performance-list has error", error);
				});

		}
	}

	private getFarmPowerLossList(active: number) {
		if (active == 0) {
			this.dateType = 3;
			let year = new Date(this.currentYear);
			this.farmTurbLossYear = [];
			this.originalFarmTurbLossYear = [];

			this.powerLossRankService.getTurbinePowerLossRankListByFarmId(this.farmId, this.powerLossType, this.dateType, year, this.rankType, this.rankValue)
				.then(res => {
					if (!res) { return false; };
					this.originalFarmTurbLossYear = [...res];
					this.farmTurbLossYear.push(...this.originalFarmTurbLossYear);

					let seriesObj: SeriesObj;

					if (this.farmTurbLossYear != null) {
						seriesObj = this.getFarmTurbColumnSeries(this.farmTurbLossYear);

						// this.yearTurbSeries = seriesObj.turbSeries;
						// this.yearTurbXAxis = seriesObj.turbXAxis.map(function (str) {
						// 	return str.replace(' ', '\n');
						// })

						this.yearTurbSeries = seriesObj.turbSeries;
						this.yearTurbXAxis = seriesObj.turbXAxis.map(function (str) {
							return str.replace(' ', '\n');
						})
					}
				})
				.catch(error => {
					console.log("app-performance-list has error", error);
				});
		} else if (active == 1) {
			this.originalFarmTurbLossQuarter = [];
			this.farmTurbLossQuarter = [];
			this.dateType = 4;
			let quarterDate: Date;

			if (this.currentQuarter == 1) {
				quarterDate = new Date(this.currentYear)
			} else if (this.currentQuarter == 2) {
				quarterDate = new Date(this.currentYear + '-' + '4')
			} else if (this.currentQuarter == 3) {
				quarterDate = new Date(this.currentYear + '-' + '7')
			} else {
				quarterDate = new Date(this.currentYear + '-' + '10')
			}

			this.powerLossRankService.getTurbinePowerLossRankListByFarmId(this.farmId, this.powerLossType, this.dateType, quarterDate, this.rankType, this.rankValue)
				.then(res => {
					if (!res) { return false; };
					this.originalFarmTurbLossQuarter = [...res];
					this.farmTurbLossQuarter.push(...this.originalFarmTurbLossQuarter);

					let seriesObj: SeriesObj;

					if (this.farmTurbLossQuarter != null) {
						seriesObj = this.getFarmTurbColumnSeries(this.farmTurbLossQuarter);

						this.qurterTurbSeries = seriesObj.turbSeries;
						this.qurterTurbXAxis = seriesObj.turbXAxis.map(function (str) {
							return str.replace(' ', '\n');
						});
					}

				})
				.catch(error => {
					console.log("app-performance-list has error", error);
				});

		}
		else {
			this.originalFarmTurbLossMonth = [];
			this.farmTurbLossMonth = [];
			let month = new Date(this.currentYear + "-" + this.currentMonth);
			this.dateType = 2;

			this.powerLossRankService.getTurbinePowerLossRankListByFarmId(this.farmId, this.powerLossType, this.dateType, month, this.rankType, this.rankValue)
				.then(res => {
					if (!res) { return false; };
					this.originalFarmTurbLossMonth = [...res];
					this.farmTurbLossMonth.push(...this.originalFarmTurbLossMonth);

					let seriesObj: SeriesObj;

					if (this.farmTurbLossMonth != null) {

						seriesObj = this.getFarmTurbColumnSeries(this.farmTurbLossMonth);

						this.monthTurbSeries = seriesObj.turbSeries;
						this.monthTurbXAxis = seriesObj.turbXAxis.map(function (str) {
							return str.replace(' ', '\n');
						});
					}

				})
				.catch(error => {
					console.log("app-performance-list has error", error);
				});

		}
	}

	private getColumnSeries(farmData: PowerLoss[], turbData: PowerLoss[]) {

		let farmSeries = [];
		let farmXAxis = [];
		let turbSeries = [];
		let turbXAxis = [];

		let farmList: PowerLoss[] = farmData;
		let turbineList: PowerLoss[] = turbData;

		let seriesDataF: FarmSeriesId[] = [];
		let seriesDataT: number[] = [];

		switch (this.powerLossType) {
			case 0:
				let farmDataActual = new List(farmList).OrderBy(o => o.actualPowerPer).ToArray().reverse();
				farmDataActual.forEach(o => {
					seriesDataF.push({ value: o.actualPowerPer, id: o.farmId });
					farmXAxis.push(o.farmName);
				})

				farmSeries.push({
					name: '实发电量',
					type: 'bar',
					data: seriesDataF
				})

				turbineList.forEach(o => {
					turbXAxis.push(o.farmName + ' ' + o.deviceName)
					seriesDataT.push(o.actualPowerPer);
				})

				turbSeries.push({
					name: '实发电量',
					type: 'bar',
					data: seriesDataT
				})
				break;
			case 1:
				let farmDataStop = new List(farmList).OrderBy(o => o.stopLossPer).ToArray().reverse();
				farmDataStop.forEach(o => {
					seriesDataF.push({ value: o.stopLossPer, id: o.farmId })
					farmXAxis.push(o.farmName);
				})

				farmSeries.push({
					name: '停机损失',
					type: 'bar',
					data: seriesDataF
				})

				turbineList.forEach(o => {
					seriesDataT.push(o.stopLossPer)
					turbXAxis.push(o.farmName + ' ' + o.deviceName)
				})
				turbSeries.push({
					name: '停机损失',
					type: 'bar',
					data: seriesDataT
				})
				break;
			case 2:
				let farmDataPerform = new List(farmList).OrderBy(o => o.performLossPer).ToArray().reverse();
				farmDataPerform.forEach(o => {
					seriesDataF.push({ value: o.performLossPer, id: o.farmId })
					farmXAxis.push(o.farmName);
				})
				farmSeries.push({
					name: '性能损失',
					type: 'bar',
					data: seriesDataF
				})

				turbineList.forEach(o => {
					seriesDataT.push(o.performLossPer)
					turbXAxis.push(o.farmName + ' ' + o.deviceName)
				})
				turbSeries.push({
					name: '性能损失',
					type: 'bar',
					data: seriesDataT
				})
				break;
			case 3:
				let farmDataLimit = new List(farmList).OrderBy(o => o.limitLossPer).ToArray().reverse();
				farmDataLimit.forEach(o => {
					seriesDataF.push({ value: o.limitLossPer, id: o.farmId })
					farmXAxis.push(o.farmName);
				})

				farmSeries.push({
					name: '限电损失',
					type: 'bar',
					data: seriesDataF
				})

				turbineList.forEach(o => {
					seriesDataT.push(o.limitLossPer)
					turbXAxis.push(o.farmName + ' ' + o.deviceName)
				})
				turbSeries.push({
					name: '限电损失',
					type: 'bar',
					data: seriesDataT
				})
				break;
			case 4:
				let farmDataBad = new List(farmList).OrderBy(o => o.badLossPer).ToArray().reverse();
				farmDataBad.forEach(o => {
					seriesDataF.push({ value: o.badLossPer, id: o.farmId })
					farmXAxis.push(o.farmName);
				})

				farmSeries.push({
					name: '坏点损失',
					type: 'bar',
					data: seriesDataF
				})

				turbineList.forEach(o => {
					seriesDataT.push(o.badLossPer)
					turbXAxis.push(o.farmName + ' ' + o.deviceName)
				})

				turbSeries.push({
					name: '坏点损失',
					type: 'bar',
					data: seriesDataT
				})
				break;
		}

		let columnDatas: SeriesObj = {
			farmSeries: farmSeries,
			farmXAxis: farmXAxis,
			turbSeries: turbSeries,
			turbXAxis: turbXAxis,
		}

		return columnDatas;

	}

	private getFarmTurbColumnSeries(turbData: PowerLoss[]) {

		let turbSeries = [];
		let turbXAxis = [];

		let turbineList: PowerLoss[] = turbData;
		let seriesDataT: number[] = [];

		switch (this.powerLossType) {
			case 0:
				turbineList.forEach(o => {
					turbXAxis.push(o.farmName + ' ' + o.deviceName)
					seriesDataT.push(o.actualPowerPer);
				})

				turbSeries.push({
					name: '实发电量',
					type: 'bar',
					data: seriesDataT
				})
				break;
			case 1:
				turbineList.forEach(o => {
					seriesDataT.push(o.stopLossPer)
					turbXAxis.push(o.farmName + ' ' + o.deviceName)
				})
				turbSeries.push({
					name: '停机损失',
					type: 'bar',
					data: seriesDataT
				})
				break;
			case 2:
				turbineList.forEach(o => {
					seriesDataT.push(o.performLossPer)
					turbXAxis.push(o.farmName + ' ' + o.deviceName)
				})
				turbSeries.push({
					name: '性能损失',
					type: 'bar',
					data: seriesDataT
				})
				break;
			case 3:
				turbineList.forEach(o => {
					seriesDataT.push(o.limitLossPer)
					turbXAxis.push(o.farmName + ' ' + o.deviceName)
				})
				turbSeries.push({
					name: '限电损失',
					type: 'bar',
					data: seriesDataT
				})
				break;
			case 4:
				turbineList.forEach(o => {
					seriesDataT.push(o.badLossPer)
					turbXAxis.push(o.farmName + ' ' + o.deviceName)
				})

				turbSeries.push({
					name: '坏点损失',
					type: 'bar',
					data: seriesDataT
				})
				break;
		}

		let columnDatas: SeriesObj = {
			farmSeries: [],
			farmXAxis: [],
			turbSeries: turbSeries,
			turbXAxis: turbXAxis,
		}

		return columnDatas;
	}
}

interface SeriesObj {
	farmSeries: any[],
	farmXAxis: any[],
	turbSeries: any[],
	turbXAxis: any[],
}

interface FarmSeriesId {
	value: number;
	id: number;
}


// WEBPACK FOOTER //
// ./src/app/module/performance/performance-list/performance-list.component.ts